/*
 * @# PhotosController.java 2011-12-23 下午03:45:09
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.px.album.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.ianswer.common.util.JsonBinder;
import com.ianswer.common.util.web.ServletUtils;
import com.ianswer.px.album.entity.Album;
import com.ianswer.px.album.entity.Photo;
import com.ianswer.px.album.io.AblumPhotoHandler;
import com.ianswer.px.album.service.AlbumService;
import com.ianswer.px.album.service.PhotoService;
import com.ianswer.px.common.SessionContextHolder;

/**
 * 相片维护.
 * 
 * @author alvin hwang
 */
@Controller
public class PhotosController {

	@Resource(name = "albumService")
	private AlbumService albumService;
	@Resource(name = "photoService")
	private PhotoService photoService;

	@RequestMapping(value = "/albums/{albumId}/detail", method = RequestMethod.GET)
	public String albumDetail(@PathVariable("albumId") Long albumId, Model model) {
		Album album = albumService.get(albumId);
		model.addAttribute("album", album);
		
		List<Photo> photos = photoService.queryAlbumPhotos(albumId, 1);
		model.addAttribute("photos", photos);
		if (album.isShowType()) {
			return "album/detail/shows";
		}
		return "album/detail/dress";
	}

	@RequestMapping(value = "/albums/{albumId}/photos/upload", method = RequestMethod.POST)
	public @ResponseBody String uploadPhotos(@PathVariable("albumId") Long albumId, MultipartHttpServletRequest request, Model model) {
		List<MultipartFile> files = request.getFiles("photos");
		Long userId = SessionContextHolder.getUserId();
		
		List<Photo> photos = new ArrayList<Photo>();
		for (MultipartFile file : files) {
			try {
				String url = AblumPhotoHandler.handle(userId, file);
				Photo photo = new Photo();
				photo.setUrl(url);
				photo.setAlbumId(albumId);
				photo.setUserId(userId);
				photo.setIpAddr(ServletUtils.getIpAddrLong(request));
				
				photoService.insert(photo);
				
				photos.add(photo);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return JsonBinder.buildNonNullBinder().toJson(photos);
	}

	@RequestMapping(value = "/albums/{albumId}/photos/{photoId}", method = RequestMethod.GET)
	public String albumDetail(@PathVariable("albumId") Long albumId, @PathVariable("photoId") Long photoId, Model model) {
		Album album = albumService.get(albumId);
		model.addAttribute("album", album);
		// TODO 这里一次加载了专辑的所有相片, 性能不好, 改为动态加载下一片相片
		List<Photo> photos = photoService.queryAlbumPhotos(albumId);
		model.addAttribute("photos", photos);
		model.addAttribute("photoId", photoId);
		if (album.isShowType()) {
			return "album/photo/shows";
		}
		return "album/photo/dress";
	}
	
	@RequestMapping(value = "/albums/{albumId}/photos/{photoId}/info", method = RequestMethod.GET)
	public @ResponseBody Photo info(@PathVariable("albumId") Long albumId, @PathVariable("photoId") Long photoId) {
		return photoService.get(photoId);
	}
}
